% GAUTIER LE BIHAN - 2020
% Replication files for "Shocks vs Menu Costs: Patterns of Price Rigidity in an Estimated Multi-Sector 
% Menu-Cost Model ?" Review of Economics and Statistics
%
% Table 3 Panel E "aggregate sector" for product level estimation of
% CalvoPlus + lepto shock
% 

clear all
close all
clear matrix
clc

addpath('..\..\Utilities')  
load actual_moments_k

load res_estim
param0=res_estim;
z=ones(2,1);
 
actual_moments=[ 0.0837  0.6737  0.0438  0.0698  4.4616;
                 0.1370 0.7096 0.0473 0.0728 5.2872];
 
actual_std=[ 0.0008  0.0042  0.0005  0.0010  0.1057;
             0.0010  0.0051 0.0007   0.0017  0.2457];
 actual_var= actual_std.* actual_std*1000;
 
 
res_estim_all_short=zeros(2,11);
for jj=1:2;
    
p0 = param0(jj,1);%param0(jj,1); 
mu_c0 =param0(jj,2);%param0(jj,2)+param0(jj,2)/5 ;
sig_eps_a0=param0(jj,3);%param0(jj,3);
p_a0 =0.75;%param0(jj,4);



mu_c_init = mu_c0;
p0_init = p0;
sig_eps_a_init = sig_eps_a0;
p_a_init = p_a0;


% Adjustement of initial search area (beyond 5 percent)

%power_p0 = 1.02;
power_mu_c = 1.0;
power_sig_eps_a = 1.0;
power_p_a = 1.0;

% save SMM

% create vector for initial parameter values
vec0 = [p0; mu_c0; sig_eps_a0; p_a0]

% Keep track of results
conv_SMM = zeros(0,3);
xopt_SMM = 100000; % start iteration
conv_opt = zeros(3,6);
SMM_count = 0; % counter

% tolerance level
tol_SMM = 1e-3;




save('SMM','vec0','p0','mu_c0','sig_eps_a0','p_a0',...
    'p0_init','mu_c_init','sig_eps_a_init','p_a_init',...
    'power_mu_c','power_sig_eps_a','power_p_a',...
    'conv_SMM','xopt_SMM','conv_opt','SMM_count',...
    'tol_SMM','-append')
% 'power_phi','phi_init','phi0'

%% Options
%  Display, TolX, TolFun, MaxFunEvals

%% Set calibration target
% std.Y, autocorr(Y), frequency of default:
moment1 = actual_moments(jj,1);
moment2 = actual_moments(jj,2);
moment3 = actual_moments(jj,3);
moment4 = actual_moments(jj,4); 
moment5 = actual_moments(jj,5);

weight_j=0.57;


target = [moment1; moment2; moment3; moment4; moment5];
save('SMM','target','-append')
%% Start minimization routine 
% options = optimset('MaxIter',20);%, 'TolX', 1e-4,'Tolfun', 1e-3);

scale=[actual_var(jj,:)'];
save('SMM','target','scale','-append')
%% Start minimization routine 
 %options = optimset('MaxIter',20);%, 'TolX', 1e-4,'Tolfun', 1e-3);
 %options = optimset('TolX', 1e-4,'Tolfun', 1e-3);
options = optimset('MaxFunEvals',50);

[x,fval, exiflag] = fminsearch(@SMMmain,vec0, options);
res_estim(jj,:)=[x' fval exiflag target']

  save res_estim res_estim;
end